﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Data;

namespace Light.Utility.HtmlTable
{
    public class HtmlTableMaker
    {
        
        private Table _table = null;
        private string sTable = string.Empty;

        
        private string GetCell(Cell c)
        {
            string str = string.Empty;
            str = "<td bgcolor=" + this.GetHexColor(c.BackColor) + " align=" + c.Align.ToString() + " style='border-top:none;border-left:none;border-bottom:solid gray 1.0pt;border-right:solid gray 1.0pt;background:" + this.GetHexColor(c.BackColor) + ";padding:1.5pt 1.5pt 1.5pt 1.5pt'><p class=MsoNormal>";
            if (c.FontSetter.Bold)
            {
                str = str + "<b>";
            }
            str = str + ("<font size=" + c.FontSetter.FontSize.ToString() + " face=" + c.FontSetter.FontFace.ToString() + " color=" + this.GetHexColor(c.FontSetter.FontColor) + "><span style='font-size:" + c.FontSetter.FontSize.ToString() + "pt;font-family:" + c.FontSetter.FontFace.ToString());
            if (c.FontSetter.Bold)
            {
                str = str + ";font-weight:bold";
            }
            if (c.FontSetter.Italic)
            {
                str = str + ";font-style:italic";
            }
            str = str + "'>";
            if (c.IsLink)
            {
                str = str + ("<a href=" + c.Url + " target=\"_blank\">");
            }
            str = str + c.Text;
            if (c.IsLink)
            {
                str = str + "</a>";
            }
            else
            {
                str = str + "&nbsp;";
            }
            str = str + "</span></font>";
            if (c.FontSetter.Bold)
            {
                str = str + "</b>";
            }
            return (str + "</p></td>\n");
        }

        private string GetColumnHeader()
        {
            string str = string.Empty;
            str = "<tr>\n";
            for (int i = 0; i < this._table.ColumnHeader.Cells.Count; i++)
            {
                str = str + this.GetCell(this._table.ColumnHeader.Cells[i]);
            }
            return (str + "</tr>\n\n");
        }

        private string GetFooter()
        {
            return "</table>";
        }

        private string GetHeader()
        {
            string str3 = "<b><font size=1 face=Tahoma color=#880000><span style='font-size:8pt;font-family:Tahoma;font-weight:bold;'>" + this._table.Title + "</span></font></b>\n\n";
            return (str3 + "<table class=MsoNormalTable border=" + this._table.Border.ToString() + " cellspacing=" + this._table.CellSpacing.ToString() + " cellpadding=" + this._table.CellPadding.ToString() + " style='border-top:solid gray 1.0pt;border-left:solid gray 1.0pt;border-bottom:none;border-right:none'></br>\n\n");
        }

        private string GetHexColor(Color c)
        {
            if (c == Color.Empty)
            {
                return "#000000";
            }
            string str = "#";
            if (c.R.ToString("X").Length == 1)
            {
                str = str + "0";
            }
            str = str + c.R.ToString("X");
            if (c.G.ToString("X").Length == 1)
            {
                str = str + "0";
            }
            str = str + c.G.ToString("X");
            if (c.B.ToString("X").Length == 1)
            {
                str = str + "0";
            }
            return (str + c.B.ToString("X"));
        }

        public string GetHtml(Table table)
        {
            this.sTable = string.Empty;
            if (table != null)
            {
                this._table = table;
                this.sTable = this.GetHeader();
                this.sTable = this.sTable + this.GetColumnHeader();
                this.sTable = this.sTable + this.GetRows();
                this.sTable = this.sTable + this.GetFooter();
            }
            return this.sTable;
        }

        public string GetHtml(DataTable dataTable, string tableTitle)
        {
            try
            {
                int num;
                Cell cell;
                if (dataTable == null)
                {
                    return string.Empty;
                }
                Table table = new Table
                {
                    Title = tableTitle,
                    ColumnCount = dataTable.Columns.Count
                };
                for (num = 0; num < table.ColumnCount; num++)
                {
                    cell = new Cell
                    {
                        Text = dataTable.Columns[num].ColumnName
                    };
                    table.ColumnHeader.Cells.Add(cell);
                }
                for (num = 0; num < dataTable.Rows.Count; num++)
                {
                    Row row = new Row();
                    for (int i = 0; i < table.ColumnCount; i++)
                    {
                        cell = new Cell();
                        try
                        {
                            cell.Text = dataTable.Rows[num][i].ToString();
                        }
                        catch
                        {
                        }
                        row.Cells.Add(cell);
                    }
                    table.Rows.Add(row);
                }
                return this.GetHtml(table);
            }
            catch
            {
                return null;
            }
        }

        private string GetRows()
        {
            string str = string.Empty;
            for (int i = 0; i < this._table.Rows.Count; i++)
            {
                str = str + "<tr>";
                for (int j = 0; j < this._table.Rows[i].Cells.Count; j++)
                {
                    str = str + this.GetCell(this._table.Rows[i].Cells[j]);
                }
                str = str + "</tr>\n\n";
            }
            return str;
        }
    }



}
